package com.skyjing.askit

import android.annotation.SuppressLint
import android.content.Context
import android.speech.tts.TextToSpeech
import android.speech.tts.TextToSpeech.OnInitListener
import java.util.*

@SuppressLint("StaticFieldLeak")
object AsText2Speech {
    private lateinit var mTextToSpeech: TextToSpeech
    private lateinit var mContext: Context
//    private

    @Synchronized
    fun init(context: Context , language: Locale = Locale.CHINESE) {
        mContext = context
//        mTextToSpeech = TextToSpeech(context,
//            OnInitListener { status: Int ->
//                LogUtils.e("语音合成：$status")
//                // 判断是否转化成功
//                if (status == TextToSpeech.SUCCESS && isTextSpeech()) {
//                    try {
//                        //默认设定语言为中文，原生的android貌似不支持中文。
//                        val result: Int = mTextToSpeech.setLanguage(Locale.CHINESE)
////                        mTextToSpeech.setPitch(0.8f)
//                        if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
//                            //不支持中文就将语言设置为英文
//                            mTextToSpeech.language = Locale.US
//                        } else {
//                            LogUtils.e("支持中文:$result")
//                        }
//                    } catch (e: Exception) {
//                        LogUtils.e(e)
//                        mTextToSpeech.language = Locale.US
//                    }
//                }
//            }
//        )

        mTextToSpeech = TextToSpeech(
            mContext,
            OnInitListener { status: Int ->
                if (status == TextToSpeech.SUCCESS && isTextSpeech()) {
                    try {
                        //设定语言
                        val result: Int = mTextToSpeech.setLanguage(language)
                        if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                            //不支持中文就将语言设置为英文
                            mTextToSpeech.language = Locale.US
                        } else {
                            LogUtils.e("支持语言:${language} $result")
                        }
                    } catch (e: Exception) {
                        LogUtils.e(e)
                        mTextToSpeech.language = Locale.US
                    }
                }
            }
        )
    }

    fun speechPitch(pitch: Float) {
        if (isTextSpeech())
            mTextToSpeech.setPitch(pitch)
    }

    fun speechRate(rate: Float) {
        if (isTextSpeech())
            mTextToSpeech.setSpeechRate(rate)
    }

    fun speechFlush(text: String?) {
        if (isTextSpeech()) {
//            mTextToSpeech.speak(
//                text,
//                TextToSpeech.QUEUE_FLUSH,
//                null
//            )

            mTextToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null, null)
        }
    }

    fun currentSpeechLanguage(): Locale? {
        return if (isTextSpeech()) {
//            mTextToSpeech.language
            mTextToSpeech.voice.locale
        } else {
            null
        }
    }

//    fun setLanguage(language: Locale) {
//        if (isContext()) {
//            mTextToSpeech = TextToSpeech(
//                mContext,
//                OnInitListener { status: Int ->
//                    if (status == TextToSpeech.SUCCESS && isTextSpeech()) {
//                        try {
//                            //设定语言
//                            val result: Int = mTextToSpeech.setLanguage(language)
//                            if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
//                                //不支持中文就将语言设置为英文
//                                mTextToSpeech.language = Locale.US
//                            } else {
//                                LogUtils.e("支持语言:${language} $result")
//                            }
//                        } catch (e: Exception) {
//                            LogUtils.e(e)
//                            mTextToSpeech.language = Locale.US
//                        }
//                    }
//                }
//            )
//        }
//    }

    fun speechAdd(text: String?) {
        if (isTextSpeech()) {
//            mTextToSpeech.speak(
//                text,
//                TextToSpeech.QUEUE_ADD,
//                null
//            )
            mTextToSpeech.speak(text, TextToSpeech.QUEUE_ADD, null, null)
        }
    }

    fun stop() {
        if (isTextSpeech()) {
            mTextToSpeech.stop()
        }
    }

    fun isSpeaking(): Boolean {
        return if (isTextSpeech()) {
            mTextToSpeech.isSpeaking
        } else false
    }

    private fun isTextSpeech(): Boolean = AsText2Speech::mTextToSpeech.isInitialized

    private fun isContext(): Boolean = AsText2Speech::mContext.isInitialized
}